package com.rodagroup.study.modules.grpc.demo.consumer.app.client;

import com.rodagroup.study.modules.grpc.demo.proto.hello.Gender;
import com.rodagroup.study.modules.grpc.demo.proto.hello.SayHelloGrpc;
import com.rodagroup.study.modules.grpc.demo.proto.hello.SayHelloRequest;
import com.rodagroup.study.modules.grpc.demo.proto.hello.SayHelloResponse;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import lombok.extern.slf4j.Slf4j;

/**
 * @author jake, {@literal <jake.kim@rodagroup.cn>}
 * @date 2022-07-07.
 */
@Slf4j
public class GrpcClient {

  private static final String SERVER_HOST = "localhost";
  private static final int SERVER_PORT = 9999;

  public static void main( String[] args ) throws Exception {
    ManagedChannel managedChannel = ManagedChannelBuilder.forAddress(SERVER_HOST, SERVER_PORT).usePlaintext().build();
    try {
      SayHelloGrpc.SayHelloBlockingStub service = SayHelloGrpc.newBlockingStub( managedChannel );
      SayHelloRequest request = SayHelloRequest
          .newBuilder()
          .setAge(22)
          .setName("WilliamWow")
          .setGender(Gender.MALE)
          .build();
      SayHelloResponse response = service.sayHello(request);

      log.info(response.getHelloMessage());
    } finally {
      managedChannel.shutdown();
    }
  }

}
